package ru.osu.acm.common.industrial;

import ru.osu.acm.common.entities.Checker;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * User: aleksey
 * Date: Sep 22, 2009
 * Time: 4:12:32 PM
 */
public class CheckerFactory extends Factory {
    public static Checker getChecker(int checkerId, Connection con, boolean closeConnection) {
        Checker checker = null;

        try {
            PreparedStatement statement = con.prepareStatement("select * from checker where id = ?");
            statement.setInt(1, checkerId);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                checker = new Checker(
                        resultSet.getInt("id"),
                        resultSet.getString("source").trim());
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return checker;
    }

    public static ArrayList<Checker> getAllCheckers(Connection con, boolean closeConnection) {
        ArrayList<Checker> checkers = new ArrayList<Checker>();

        try {
            PreparedStatement statement = con.prepareStatement("select * from checker order by id");
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                checkers.add(new Checker(
                        resultSet.getInt("id"),
                        resultSet.getString("source").trim()));
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return checkers;
    }
}
